package com.glueball.crm.module.client.app;

import org.fusesource.restygwt.client.JsonEncoderDecoder;

import com.glueball.crm.module.client.model.Client;
import com.glueball.crm.module.client.ui.ClientUI;
import com.glueball.crm.module.util.CrmRequest;
import com.glueball.crm.module.util.CrmRequestCallback;
import com.glueball.crm.module.util.URLHelper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;

public class ClientApp extends Composite implements EntryPoint {

	private final ClientUI ui = new ClientUI(this);

	public interface ClientCodec extends JsonEncoderDecoder<Client> {
	}

	private final ClientCodec codec = GWT.create(ClientCodec.class);

	private final CrmRequest<Client> req = new CrmRequest<Client>(codec);

	public void onModuleLoad() {
		RootPanel.get().add(ui);
		final String id = Window.Location.getParameter("id");
		try {
			req.getEntity(URLHelper.GET_CLIENT+id, new CrmRequestCallback<Client>(){
				public void onSuccess(Client entity) {
					try {
						ui.setClient((Client)entity);
						ui.refresh();
					} catch (Exception e) {
						Window.alert(this.getClass().getName()+" : "+e.getMessage());
					}
				}
			});
		} catch (final Exception e) {
			Window.alert(this.getClass().getName()+" : "+e.getMessage());
		}
	}
}
